package com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.pictureactivity;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.PicturePagerAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.bean.staritembean.PictureListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/13.
 * 图集展览
 */
public class PictureActivity extends BaseActivity implements PictureContract.View {
    @InjectView(R.id.vp_pic)
    ViewPager mViewPager;
    @InjectView(R.id.pic_back)
    ImageView picBack;
    @InjectView(R.id.pic_page)
    TextView picPage;
    private List<ImageView> images = new ArrayList<>();
    private ImageView mImageView;
    private SimpleDraweeView view;
    private List<PictureListBean> list = new ArrayList<>();
    private int position;
    private ProgressBar pb;

    @Override
    public int getLayoutId() {
        return R.layout.activity_picture;
    }

    @Override
    public void getPictureListSuccess() {

    }

    @Override
    public void getPictureListFail(String msg) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        pb= (ProgressBar) findViewById(R.id.progressBar);
        list = (List<PictureListBean>) getIntent().getSerializableExtra("list");
        position = getIntent().getIntExtra("position", 0);
        //添加数据
        for (int i = 0; i < list.size(); i++) {
            mImageView = new ImageView(this);
            Glide.with(this).load(list.get(i).getImg()).into(new GlideDrawableImageViewTarget(mImageView){
                //重写其中的三个方法，其实就是对加载过程进行监听！
                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
                    super.onLoadFailed(e, errorDrawable);
                    //隐藏加载的进度条
                    pb.setVisibility(View.GONE);
                }

                @Override
                public void onLoadStarted(Drawable placeholder) {
                    super.onLoadStarted(placeholder);
                    //显示加载进度条
                    pb.setVisibility(View.VISIBLE);
                }

                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                    super.onResourceReady(resource, animation);
                    //代表网络图片加载成功---->隐藏加载的进度条
                    pb.setVisibility(View.GONE);
                }
            });
//            view=new SimpleDraweeView(this);
//            ImageRequest request= ImageRequestBuilder.newBuilderWithSource(Uri.parse(list.get(i).getImg())).
//                    setProgressiveRenderingEnabled(true).build();
//            PipelineDraweeController controller= (PipelineDraweeController) Fresco.newDraweeControllerBuilder().setImageRequest(request)
//                    .setOldController(view.getController()).build();
//            view.setController(controller);
            images.add(mImageView);
//            images.add(view);
        }
        picPage.setText((position+1)+"/"+list.size());
        //viewpager初始化
        mViewPager.setAdapter(new PicturePagerAdapter(images));
        mViewPager.setCurrentItem(position);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                picPage.setText((position+1)+"/"+list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        picBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}